<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<?php echo @$css.@$js;?>		
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>	
<body>
<div id="addOrder" class="win_div" style="display:block;">
<div class="win_title">下订单<a href="javscript:;" onclick="parent.closeWin();" class="frame_close">[关闭] </a></div>
<div class="win_con">
<form method="post" action="<?php echo ZSM_WWW_HOST ?>/admin/api/addorder/do/add" onsubmit="return checkForm();">
<table width="100%">


<tr class="order_good_list_tr"><td colspan="2">
<table>
<?php $goodsId = '';?>
<?php foreach ($goods as $k=>$v):?>
<tr id="tr_good_<?php echo $v['goods_id'];?>">
<td align="right">产品名称：</td><td><?php  echo $v['goods_name'];?><input type="hidden" id="goods_name_<?php echo $v['goods_id'];?>" value="<?php echo $v['goods_name']?>"/></td>
<td align="right">产品数量：</td><td><input type="text" id="goods_nums_<?php echo $v['goods_id'];?>" style="width:30px;" value="1"/></td>
<td align="right">出售单价：</td><td><input type="text" id="order_price_<?php echo $v['goods_id'];?>" style="width:70px;" value="<?php echo $v['sell_price']?>"/></td>
<td>
<?php if (count($goods)>1){?><a href="javascript:;" onclick="cancelGood('<?php echo $v['goods_id'];?>')">删除</a><?php }?>

</td>
</tr>
<?php $goodsId = $goodsId.','.$v['goods_id'].','?>
<?php endforeach;?>
</table>
<input type="hidden" value="<?php echo $goodsId;?>" id="goods_ids" />

</td>
</tr>
<tr>
<td class="td_30">配送方式：</td>
<td>
<select id="ship_id" name="ship_id" >
<option value="0">选择配送方式</option>
<?php 
foreach ($ship as $k=>$v):
echo '<option value="'.$v['ship_id'].'">'.$v['ship_name'].'</option>';
endforeach;?>
</select>
</td>
</tr>
<tr>
<td class="td_30">客户姓名：</td>
<td>
<input type="text" id="client_name" style="width:70px;"/>
<select id="client_id" name="client_id" onchange="setClient()">
<option value="0">选择我的客户</option>
<?php 
foreach ($clients as $k=>$v):
echo '<option value="'.$v['client_id'].'">'.$v['client_name'].'</option>';
endforeach;?>
</select>
&nbsp;
<input type="button" value="添加新客户" onclick ="addNewClient()">
</td>
</tr>

<tr>
<td class="td_30">客户电话：</td>
<td>
<input type="text" id="client_tel" style="width:120px;"/>
</td>
</tr>

<tr>
<td class="td_30">地址：</td>
<td>
<textarea style="width:200px;height:50px;" id="client_address"></textarea>
</td>
</tr>

<tr><td class="td_30">
<input type="submit"  value="下单" />
</td>
<td>&nbsp;</td>
</tr>
</table>
</form>
</div>
</div>
<script type="text/javascript">
function cancelGood(goodsId)
{
	$("#tr_good_"+goodsId).remove();
	var goods_ids = $("#goods_ids").val();

	$("#goods_ids").val(goods_ids.replace(','+goodsId+',',''));
}
function checkForm()
{
	
	var goods_ids = $("#goods_ids").val();
	var strs = goods_ids.split(",");
	var s='';
	for(var i = 0; i < strs.length; i++) {	
		if(strs[i]){
			s = s + ";"+ strs[i] + "," + $("#goods_name_"+strs[i]).val() + "," + $("#goods_nums_"+strs[i]).val() + "," + $("#order_price_"+strs[i]).val();	
			
		}
	}
	if(!s){
		parent.showAlert('必须有产品才能下单！');
		return false;
	}
	var ship_id = $.trim($("#ship_id").val());	
	var client_name = $.trim($("#client_name").val());
	var client_tel = $.trim($("#client_tel").val());
	var client_address = $.trim($("#client_address").val());
	
	
	if(ship_id=='0' || client_name=='' || client_tel=='' || client_address==''){
		parent.showAlert('未选配送方式或客户资料不全！');
		return false;
	}	
	var url = "<?php echo ZSM_WWW_HOST.'admin/api/addorder/do/add'?>";
	$.getJSON(url,{
		's':s,
		'ship_id':ship_id,		
		'client_name':client_name,
		'client_tel':client_tel,
		'client_address':client_address
		},function(rs){
			parent.showAlert(rs.html);
			setTimeout(function(e){
				parent.closeWin();
			},3000);
		});
	return false;

}
function addNewClient()
{
	
	var url = "<?php echo ZSM_WWW_HOST.'admin/api/addnewclient/'?>";
	showFrame(url,370,200);
	
}
function reloadClient()
{
	var url = "<?php echo ZSM_WWW_HOST.'admin/api/getclients';?>";
	$.getJSON(url,function(rs){
		$("#client_id").html(rs.html);
		setClient();
	})
	
}
function setClient()
{
	var clientId = $("#client_id").val();
	
	var url = "<?php echo ZSM_WWW_HOST.'admin/api/getclients/client_id/';?>"+clientId;
	
	$.getJSON(url,function(rs){
		$("#client_name").val(rs.client_name);
		$("#client_tel").val(rs.client_tel);
		$("#client_address").val(rs.client_address);
	})
}
</script>
</body>
</html>
